Certainly! In Flutter, the Visibility widget allows you to control the visibility of its child widget by toggling between visible and invisible states. It's often used to conditionally show or hide specific UI elements.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
bool isVisible = true;
void toggleVisibility() {
setState(() {
isVisible = !isVisible;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Visibility Widget Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
onPressed: toggleVisibility,
child: Text('Toggle Visibility'),
),
Visibility(
visible: isVisible,
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(
child: Text(
'Visible Widget',
style: TextStyle(color: Colors.white),
),
),
),
),
],
),
),
),
);
}
}